package com.itheima.service.db.impl;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.Announcement;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.mapper.AnnouncementMapper;
import com.itheima.mapper.UserInfoMapper;
import com.itheima.service.db.AnnouncementService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Calendar;
import java.util.List;

@Service
public class AnnouncementServiceImpl implements AnnouncementService {

    @Autowired
    AnnouncementMapper announcementMapper;

    @Autowired
    UserInfoMapper userInfoMapper;


    @Override
    public PageBeanVo findByPage(Integer pageNum, Integer pageSize, int commentType) {
        //1.开启分页(设置分页参数)
        Page<Announcement> page = new Page<>(pageNum, pageSize);

        //2.执行分页查询
        page = (Page<Announcement>) announcementMapper.selectPage(page, null);

        //4.封装pageBeanVo且返回
        return new PageBeanVo(pageNum,pageSize,page.getTotal(),page.getRecords());
    }

    @Override
    public List<UserInfo> announcementBirthday() {

        //获取当前时间
        Calendar cal = Calendar.getInstance();

        //获取当前时间的年,月,日
        //int yearNow = cal.get(Calendar.YEAR);
        int mothNow = cal.get(Calendar.MONTH) + 1;
        int dateTomothNow = cal.get(Calendar.DAY_OF_MONTH);
        String month = NumberUtil.toStr(mothNow);
        String date = NumberUtil.toStr(dateTomothNow);
        if (mothNow < 10) {
            month = "0" + mothNow;
        } else if (dateTomothNow < 10) {
            date = "0" + dateTomothNow;
        }

        QueryWrapper<UserInfo> qw = new QueryWrapper<>();
        qw.likeLeft("birthday", month + "-" + date);
        List<UserInfo> userInfoList = userInfoMapper.selectList(qw);

        return userInfoList;
    }
}
